package com.bdqn.tour.servlet.Gyh;

import com.bdqn.tour.service.GyhAddUserInfoService;
import com.bdqn.tour.service.impl.GyhAddUserInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * 添加用户信息实体类
 */
@WebServlet("/GyhaddUserInfo")
public class GyhAddUserInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取参数
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        String name = UUID.randomUUID().toString();
        //调用service业务层数据GyhAddUserInfoServiceImpl
        GyhAddUserInfoService service = new GyhAddUserInfoServiceImpl();
        //调用业务层添加用户方法
        int result = service.addUserInfo(phone, password, name);
        //判断是否添加成功result大于0为成功 等于0为失败
        if (result > 0) {
            //向客户端发送成功结果success
            resp.getWriter().print("success");
        } else {
            //向客户端发送失败结果error
            resp.getWriter().print("error");
        }
        //关闭字符输出流
        resp.getWriter().close();
    }
}
